package com.videoplat.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

/**
 * 上传文件和下载文件类
 * @author Administrator
 *
 */
public class UpLoadAndDownLoadFile {

    /**
     * 下载文件
     * @param file
     * @param response
     */
    public void downLoadFile(File file,String name, HttpServletResponse response) {
        InputStream is = null;
        OutputStream out = null;
        try {
            // 取得文件的后缀名。
            // String ext = file.getName().substring(file.getName().lastIndexOf(".")).toLowerCase();

            // 清空response
            response.reset();

            // 设置response的Header
            String fileName = new String(name.substring(0).getBytes("GBK"), "ISO_8859_1");
            response.setContentType("application/text;charset=GBK");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            is = new BufferedInputStream(new FileInputStream(file));
            byte[] buffer = new byte[8192];
            int lenth = 0;
            out = response.getOutputStream();
            while ((lenth = is.read(buffer)) > 0) {
                out.write(buffer, 0, lenth);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (out != null) {
                    out.flush();
                    out.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }

    }

    /**
     * 创建文件夹
     * @param fileDir  文件夹创建的路径
     */
    public void mkDir(String fileDir) {
        try {
            java.io.File file = new java.io.File(fileDir);
            if (!file.exists()) {
                if (!file.mkdirs()) {
                    throw new Exception("创建文件夹失败!");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 上传文件
     * @param ff  上传的文件
     * @param fileDir  文件路径
     * @return  fileName  返回系统保存的文件名
     */
    public void upLoadFile(File ff, String fileName,String url) {
        mkDir(url);
        InputStream stream;
        OutputStream bos;
        try {
            stream = new FileInputStream(ff);
            bos = new FileOutputStream(url + "\\" + fileName);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
                bos.write(buffer, 0, bytesRead);
            }
            bos.close();
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void copy(String from,String to) throws IOException{
        FileInputStream in=new FileInputStream(from);
        File file=new File(to);
        if(!file.exists())
            file.createNewFile();
        FileOutputStream out=new FileOutputStream(file);
        int c;
        byte buffer[]=new byte[1024];
        while((c=in.read(buffer))!=-1){
            for(int i=0;i<c;i++)
                out.write(buffer[i]);        
        }
        in.close();
        out.close();
    }

}
